﻿using System;
using System.Collections.Specialized;
using System.Text;

namespace AviationSimulator.UDP
{
    class ModelMessage
    {
        #region Properties

        /// <summary>
        /// Параметр заголовка сообщения (Get, Set)
        /// </summary>
        public string Header_T { get; set; }

        /// <summary>
        /// Параметр заголовка сообщения (Get, Set)
        /// </summary>
        public string Header_S { get; set; }

        /// <summary>
        /// Коллекция параметров сообщения (Get, Set)
        /// </summary>
        public HybridDictionary Parameters { get; set; }

        /// <summary>
        /// Кол-во параметров в сообщении (Get)
        /// </summary>
        public int ParamsCount
        {
            get { return this.Parameters.Count; }
        }

        private string _message;
        
        #endregion

        #region ctors

        public ModelMessage()
        {
            this._message = string.Empty;
            this.Header_T = string.Empty;
            this.Header_S = string.Empty;
            this.Parameters = new HybridDictionary();
        }

        public ModelMessage(string modelMessage)
        {
            this._message = modelMessage;
            this.Parameters = new HybridDictionary();
            ParseString();
        } 

        #endregion
        
        #region Public Methods

        /// <summary>
        /// Получение значения пар-ра по ключу
        /// </summary>
        /// <param name="ParameterName"></param>
        /// <returns></returns>
        public object GetParameter(object ParameterName)
        {
            object Result = null;

            if (this.Parameters.Contains(ParameterName))
            {
                Result = this.Parameters[ParameterName];
            }

            return Result;
        }

        /// <summary>
        /// Добавление пар-ра в коллекцию
        /// </summary>
        /// <param name="ParameterName"></param>
        /// <param name="ParameterValue"></param>
        public void AddParameter(object ParameterName, object ParameterValue)
        {
            if (!this.Parameters.Contains(ParameterName))
            {
                this.Parameters.Add(ParameterName, ParameterValue);
            }

        }

        /// <summary>
        /// Удаление пра-ра из коллекции
        /// </summary>
        /// <param name="ParameterName"></param>
        public void RemoveParameter(object ParameterName)
        {
            if (this.Parameters.Contains(ParameterName))
            {
                this.Parameters.Remove(ParameterName);
            }
        }

        /// <summary>
        /// Построение XML сообщения
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append("<HD><T>" + this.Header_T + "</T><S>" + this.Header_S + "</S></HD><P>");

            foreach (object var in this.Parameters.Keys)
            {
                Result.Append("<PN>" + var.ToString() + "</PN><V>" + this.Parameters[var].ToString() + "</V>");
            }

            Result.Append("</P>");

            this._message = Result.ToString();

            return this._message;
        } 

        #endregion

        #region Private Methods

        private void ParseString()
        {
            this.PrecessHeader();

            this.ProcessParameters();
        }

        private void ProcessParameters()
        {
            if (this._message.Contains("<P>"))
            {
                string strParams = this._message.Substring(this._message.IndexOf("<P>"));

                int Index = 0;

                while (strParams.IndexOf("<PN>", Index) != -1)
                {
                    string ParamName = string.Empty;
                    string ParamValue = string.Empty;

                    ParamName = strParams.Substring(
                        strParams.IndexOf("<PN>", Index) + 4,
                        strParams.IndexOf("</PN>", Index) - 4 - strParams.IndexOf("<PN>", 0));
                    ParamValue = strParams.Substring(
                        strParams.IndexOf("<V>", Index) + 3, strParams.IndexOf("</V>", Index) - 3 - strParams.IndexOf("<V>", 0));

                    strParams = strParams.Substring(strParams.IndexOf("</V>") + 4);

                    if (!this.Parameters.Contains(ParamName))
                    {
                        this.Parameters.Add(ParamName, ParamValue);
                    }
                }
            }
        }

        private void PrecessHeader()
        {
            if (this._message.Contains("<T>"))
            {
                this.Header_T = this._message.Substring(
                    this._message.IndexOf("<T>") + 3, this._message.IndexOf("</T>") - 3 - this._message.IndexOf("<T>"));
            }

            if (this._message.Contains("<S>"))
            {
                this.Header_S = this._message.Substring(
                    this._message.IndexOf("<S>") + 3, this._message.IndexOf("</S>") - 3 - this._message.IndexOf("<S>"));
            }
        }

        #endregion
    }
}
